import java.util.List;
import java.util.Scanner;

public class UserManager {
    public void registerUser(List<RegularUser> users, Scanner scanner) {
        System.out.print("请输入用户名（长度不少于5个字符）: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码（长度大于8个字符，由大小写字母、数字和特殊符号组成）: ");
        String password = scanner.nextLine();
        System.out.print("请输入邮箱: ");
        String email = scanner.nextLine();
        System.out.print("请输入会员等级: ");
        String membershipLevel = scanner.nextLine();
        System.out.print("请输入电话号码: ");
        String phoneNumber = scanner.nextLine();

        if (isUsernameValid(username) && isPasswordValid(password)) {
            RegularUser newUser = new RegularUser(username, password, email, membershipLevel, phoneNumber);
            users.add(newUser);
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败，用户名或密码不符合要求。");
        }
    }

    public RegularUser loginUser(List<RegularUser> users, Scanner scanner) {
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        for (RegularUser user : users) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null; // 登录失败
    }

    public void userFunctionality(RegularUser user, List<Product> products, Scanner scanner) {
        boolean inUserFunctionality = true;
        while (inUserFunctionality) {
            System.out.println("客户功能菜单");
            System.out.println("1. 修改密码");
            System.out.println("2. 忘记密码");
            System.out.println("3. 购物");
            System.out.println("4. 查看购物历史");
            System.out.println("5. 退出登录");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    changePassword(user, scanner);
                    break;
                case 2:
                    resetPassword(user, scanner);
                    break;
                case 3:
                    shopping(user, products, scanner);
                    break;
                case 4:
                    viewShoppingHistory(user);
                    break;
                case 5:
                    inUserFunctionality = false; // 退出登录
                    System.out.println("已退出客户登录。");
                    break;
                default:
                    System.out.println("无效的选择。");
            }
        }
    }

    private void changePassword(RegularUser user, Scanner scanner) {
        System.out.print("请输入新密码: ");
        String newPassword = scanner.nextLine();
        if (isPasswordValid(newPassword)) {
            user.setPassword(newPassword);
            System.out.println("密码已更新。");
        } else {
            System.out.println("密码不符合要求。");
        }
    }

    private void resetPassword(RegularUser user, Scanner scanner) {
        System.out.print("请输入注册邮箱: ");
        String email = scanner.nextLine();
        if (email.equals(user.getEmail())) {
            String newPassword = generateRandomPassword();
            user.setPassword(newPassword);
            System.out.println("新密码已发送到您的邮箱: " + newPassword);
        } else {
            System.out.println("邮箱不匹配。");
        }
    }

    public void shopping(RegularUser user, List<Product> products, Scanner scanner) {
        System.out.println("可购买的商品：");
        for (Product product : products) {
            System.out.println("商品编号: " + product.getProductId() + ", 商品名称: " + product.getName() +
                    ", 零售价格: " + product.getRetailPrice() + ", 数量: " + product.getQuantity());
        }
        System.out.print("请输入要购买的商品编号: ");
        String productId = scanner.nextLine();
        Product productToBuy = findProductById(products, productId);
        if (productToBuy != null && productToBuy.getQuantity() > 0) {
            System.out.print("请输入购买数量: ");
            int quantity = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            if (quantity <= productToBuy.getQuantity()) {
                productToBuy.setQuantity(productToBuy.getQuantity() - quantity);
                user.addToShoppingHistory("购买商品: " + productToBuy.getName() + ", 数量: " + quantity);
                user.addToTotalSpent(productToBuy.getRetailPrice() * quantity); // 更新总支出
                System.out.println("购买成功！");
            } else {
                System.out.println("库存不足。");
            }
        } else {
            System.out.println("未找到商品或库存不足。");
        }
    }

    private Product findProductById(List<Product> products, String productId) {
        for (Product product : products) {
            if (product.getProductId().equals(productId)) {
                return product;
            }
        }
        return null; // 未找到商品
    }

    private void viewShoppingHistory(RegularUser user) {
        System.out.println("购物历史：");
        if (user.getShoppingHistory().isEmpty()) {
            System.out.println("没有购物记录。");
        } else {
            for (String record : user.getShoppingHistory()) {
                System.out.println(record);
            }
        }
    }

    private boolean isUsernameValid(String username) {
        return username.length() >= 5;
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 8 && password.matches(".*[A-Z].*") && password.matches(".*[a-z].*")
                && password.matches(".*[0-9].*") && password.matches(".*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?].*");
    }

    private String generateRandomPassword() {
        // 生成随机密码的逻辑
        return "RandomPassword123!"; // 示例
    }
}
